#include "CaptureImageCallback.h"
#include <osg/RenderInfo>
#include "Camera.h"
//#include "opencv2/imgproc/imgproc.hpp"
//#include "opencv2/imgproc/imgproc_c.h"
//#include "opencv2/core/internal.hpp"


using namespace IG;
CaptureImageCallback::CaptureImageCallback(Camera* cam)
{
	_image = new osg::Image;
	img = cv::Mat(cvSize(320 , 240), CV_8UC3);
	outImg = cv::Mat(cvSize(320, 240), CV_8UC3);
	lockIpl.Unlock();
	dWidth = 320;
	dHeight = 240;
	irProcess = false;
	////_cam = cam;
	//buffer = new unsigned char[640 * 480 * 3 * 1];

}


CaptureImageCallback::~CaptureImageCallback()
{
}
 void CaptureImageCallback::operator () (osg::RenderInfo& renderInfo) 	 const
	{

	 glReadBuffer(GL_BACK);


		//OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
		osg::GraphicsContext* gc = renderInfo.getState()->getGraphicsContext();
		if (gc->getTraits())
		{
			GLenum pixelFormat;

			//if (gc->getTraits()->alpha)
			//	pixelFormat = GL_RGBA;
			//else
				pixelFormat = GL_BGR;


			int width = gc->getTraits()->width;
			int height = gc->getTraits()->height;
			if (img.size().width != width || img.size().height != height)
			{
				//img.resize(cvSize(width, height));
				img = cv::Mat(cvSize(width, height), CV_8UC3);
				
			}

				   
			_image->readPixels(0, 0, width, height, pixelFormat, GL_UNSIGNED_BYTE);
//			memcpy(buffer, _image->data(), 640 * 480 * 3 * 1);
			_image->flipVertical();
			while (lockIpl.IsLocked()) Sleep(1);
			lockIpl.Lock();
			memcpy(img.data, _image->data(), width*height * 3 * 1);
//			_cam->outImage = img;
			lockIpl.Unlock();


		}
 }
cv::Mat CaptureImageCallback::GetImage()
{
	 
	 while (lockIpl.IsLocked()) Sleep(1);
	 lockIpl.Lock();
	 if ((img.size().width != outImg.size().width) || (img.size().height != outImg.size().height))
	 {
		 //outImg = cv::Mat(img.size(), CV_8UC3);
		 //outImg.resize(img.size());
		 //cvResize(img, outImg);
		 int dilation_size = 2;
		cv:: Mat element = cv::getStructuringElement(2/*MORPH_ELLIPSE*/,
			 cv::Size(2 * dilation_size + 1, 2 * dilation_size + 1),
			cv::Point(dilation_size, dilation_size));
		 /// Apply the dilation operation
		 dilate(img, img, element);
		 //enum
		 //{
			// CV_INTER_NN = 0,
			// CV_INTER_LINEAR = 1,
			// CV_INTER_CUBIC = 2,
			// CV_INTER_AREA = 3,
			// CV_INTER_LANCZOS4 = 4
		 //};
		 resize(img, outImg, outImg.size(), 0, 0,4);  //INTER_LINEAR
	 }
	 else
		 memcpy(outImg.data, img.data, img.size().width*img.size().height * 3 * 1);
	 //img.convertTo(outImg, CV_BGR2RGB);
	 //cv::cvtColor(img, outImg, CV_BGR2RGB);
	 lockIpl.Unlock();
	 return outImg;
	 
}
		//if (!_fileName.empty())
		//{
		//	std::cout << "Writing to: " << _fileName << std::endl;
		//	osgDB::writeImageFile(*_image, _fileName);
		//}
 void CaptureImageCallback::Print()
 {
	 printf("Hello World\n");
 }
 void CaptureImageCallback::SetDesiredSize(int width, int height, bool ir)
 {
	 while (lockIpl.IsLocked()) Sleep(1);
	 lockIpl.Lock();

	 dWidth = width;
	 dHeight = height;
	 irProcess = ir;



	 if ((dWidth != outImg.size().width) || (dHeight != outImg.size().height))
	 {
		 outImg = cv::Mat(cvSize(dWidth, dHeight), CV_8UC3);
		// outImg.resize(img.size());
		// resize(outImg, outImg, cv::Size(dWidth, dHeight));
	 }


	 lockIpl.Unlock();
 }